package cartagows.wsframework.core;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

/**
 * Singleton thread-safe used for the creation of SOAP Fault
 * @author Andrea Santi
 *
 */

public class FaultBuilder {

	private OMFactory factory;
	
	private static class Container {
		public static FaultBuilder instance = new FaultBuilder();
	}
	
	public static FaultBuilder getInstance(){
		return Container.instance;
	}

	private FaultBuilder(){
		factory = OMAbstractFactory.getOMFactory();
	}
	
	public OMElement buildSOAPFault(String code,String subCode, String reason,String detail){

		OMNamespace wsoap12 = factory.createOMNamespace("http://schemas.xmlsoap.org/wsdl/soap12/", "wsoap12");

		OMElement fault = factory.createOMElement("Fault",wsoap12);
		OMElement codeEl = factory.createOMElement("Code",wsoap12);
		OMElement codeValueEl = factory.createOMElement("Value",wsoap12);
		OMElement subCodeValueEl = factory.createOMElement("Value",wsoap12);
		OMElement reasonTextEl = factory.createOMElement("Text",wsoap12);
		
		OMElement subCodeEl = factory.createOMElement("SubCode",wsoap12);
		OMElement reasonEl = factory.createOMElement("Reason",wsoap12);
		OMElement detailEl = factory.createOMElement("Detail",wsoap12);
	
		codeValueEl.setText(code);
		subCodeValueEl.setText(subCode);
		subCodeEl.addChild(subCodeValueEl);
		codeEl.addChild(codeValueEl);
		codeEl.addChild(subCodeEl);
		
		reasonTextEl.setText(reason);
		reasonEl.addChild(reasonTextEl);
		detailEl.setText(detail);

		fault.addChild(codeEl);
		fault.addChild(reasonEl);
		fault.addChild(detailEl);
		return fault;
	}
}